
$(function() {
	
	$("#from_s:visible").autocomplete(Test.emp);		
});

var Message = {

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"发送": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_add")[0];
	
				var body = { };
				body.to = form.to.value;
				body.subject = form.subject.value;				
				body.content = form.content.value;
	
				$(this).dialog("close");
	
				$.post('log.php?do=MessageMy.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'log.php?do=MessageMy.edit&id=' + id;
		var c2 = 'log.php?do=MessageMy.view&id=' + id;
				
		var cs = 'log.php?do=MessageMy.status&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'Message.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
				
		// 未读
		if (status == 1) {
			var s = c1 + ", " + c2;

			return popup(e, [a1, a2], s);
		}

		// 已读
		if (status == 2) {		
			var s = c2;

			return popup(e, [a2], s);
		}		
			
		var s = c2;

		return popup(e, [a2], s);
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('log.php?do=MessageMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() {
						$(this).dialog("close");
					},
			
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
			
						var body = {};
						body.id = form.id.value;
						body.purpose = form.purpose.value;
						body.address = form.address.value;
						body.t1 = form.date1.value + " " + form.time1.value;
						body.t2 = form.date2.value + " " + form.time2.value;
						
						body.staff = form.staff.value;		
						body.material = form.material.value;
						
						body.content = form.content.value;
			
						$(this).dialog("close");
			
						var url = 'log.php?do=MessageMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}
};
